package leetcode.lt100;

import java.util.Arrays;

class Solution_80 {

//    给定 nums = [1,1,1,2,2,3],
//
//    函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3 。
//
//    你不需要考虑数组中超出新长度后面的元素。

    public static void main(String[] args) {
        int[] nums = new int[]{0, 0, 1, 1, 1, 1, 2, 3, 3};
        int i1 = removeDuplicates(nums);
        Arrays.stream(nums).forEach(System.out::print);
    }

    public static int removeDuplicates(int[] nums) {
        int l = nums.length;
        if (l <= 2) {
            return l;
        }
        int delete = 0;
        for (int i = 2; i < l; i++) {
            int beforeOne = nums[i - 1 - delete];
            int beforeTwo = nums[i - 2 - delete];
            if (nums[i] == beforeOne && nums[i] == beforeTwo) {
                delete++;
            } else {
                nums[i - delete] = nums[i];
            }
        }
        return l - delete;
    }
}